/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cordys.uc.webapps.translator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

/**
 *
 * @author srkrishnan
 */
public class FileUtil {

    static int getLineNo(int start, String tmp, String html) {

        return getNewLines(html.substring(0, start + 1));
    }

    static int getNewLines(String str) {
        return str.split("\r\n|\r|\n").length;
    }

    static String getLine(int lno, String str) {

        return str.split("\n")[lno - 1];

    }

    static String getjsFile(String filename) {

        String line = "", tmp = "";

        try {
            if (!new File(filename).exists()) {
                System.err.println("FILE NOT FOUND: " + new File(filename).getAbsolutePath());
                return "";
            }
            FileReader input = new FileReader(filename);
            BufferedReader bufRead = new BufferedReader(input);

            while ((line = bufRead.readLine()) != null) {

                tmp += line + "\n";

            }

        } catch (IOException ex) {
        }

        return tmp;

    }

    public static String getFile(String filename) {

        String line = "", tmp = "",strhead="";
        boolean flg = false;

        try {
            //get file reader
            FileReader input = new FileReader(filename);
            BufferedReader bufRead = new BufferedReader(input);
            LineNumberReader lnreader = HTMLTranslator.getLnreader();
            lnreader = new LineNumberReader(new FileReader(filename));
            HTMLTranslator.setLnreader(lnreader);
            while ((line = bufRead.readLine()) != null) {
                if (line.toLowerCase().contains("<body")) {
                    flg = true;
                }
                if (flg) {
                    tmp += line + "\n";
                } else {
                    
                    strhead += line + "\n";
                    
                }
            }
            HTMLTranslator.setStrhead(strhead);
            
        } catch (IOException ex) {
            
        }

        return tmp;

    }
}
